package com.yc.baihe.biz.impl;

import com.yc.baihe.beans.AdminUser;
import com.yc.baihe.beans.BhUser;
import com.yc.baihe.biz.IBhUserBiz;
import com.yc.baihe.dao.DBHelper;
import com.yc.baihe.dao.MD5Encryption;

import java.text.SimpleDateFormat;
import java.util.*;

import oracle.sql.DATE;

public class BhUserBizImpl implements IBhUserBiz{
	DBHelper db;

	public BhUserBizImpl(){
		db = new DBHelper();
	}

	public int addBhUser(BhUser bu){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		bu.setRegDate(sdf.format(new Date()));
		String sql = "insert into bhuser values(seq_bhuser_buid.nextval,?,?,to_date(?,'yyyy-MM-dd'),1)";
		List<Object> params = new ArrayList<Object>();
		params.add(MD5Encryption.createPassword(bu.getPwd()));
		params.add(bu.getBhemail());
		params.add(bu.getRegDate());
		System.out.println(bu.getBhemail()+"&&"+bu.getRegDate()+"&&"+bu.getPwd());
		return db.update(sql, params);
	}

	public int checkEmail(String bhemail){
		String sql = "select bhemail from bhuser where bhemail=?";
		List<Object> params = new ArrayList<Object>();
		params.add(bhemail);
		List<List<String>> list = db.finds(sql, params);
		return list.size();
	}

	public List<String> getAllEmail(){
		String sql = "select bhemail from bhuser";
		List<List<String>> list = db.finds(sql, null);
		List<String> result = null;
		if(list!=null && list.size()>0){
			result=new ArrayList<String>();
			for(List<String> ls:list){
				for(String s:ls){
					result.add(s);
				}
			}
		}
		return result;
	}

	public BhUser loginBhUser(String email, String pwd){
		String sql = "select * from bhuser where bhemail=? and pwd=?";
		List<Object> params = new ArrayList<Object>();
		params.add(email);
		params.add(MD5Encryption.createPassword(pwd));
		List<BhUser> list = db.find(sql, params,BhUser.class);
		//System.out.println(email+"---"+MD5Encryption.createPassword(pwd)+"---"+list);
		if (list != null && list.size() > 0)
			return (BhUser)list.get(0);
		else
			return null;
	}

	public int UpdatePwd(String buid, String oldPwd, String newPwd){
		String sql = "update bhuser set pwd=? where usid=? and pwd=?";
		List<Object> params = new ArrayList<Object>();
		params.add(MD5Encryption.createPassword(newPwd));
		params.add(buid);
		params.add(MD5Encryption.createPassword(oldPwd));
		return db.update(sql, params);
	}

	public int UpdatePhoto(String buid, String photo){
		return 0;
	}

	public int update(BhUser bu){
		return 0;
	}

	public int delBhUser(String buid){
		if (buid.indexOf("or") >= 0){
			return 0;
		} else{
			String sql = (new StringBuilder("update bhuser set status=0 where usid in(")).append(buid).append(")").toString();
			return db.update(sql, null);
		}
	}

	public String findBuidFromEmail(String email) {
		String sql="select buid from bhuser where bhemail=?";
		List<Object> params = new ArrayList<Object>();
		params.add(email);
		return db.finds(sql, params).get(0).get(0);
	}
	
	public String findEmailFromBuid(int buid) {
		String sql="select bhemail from bhuser where buid=?";
		List<Object> params = new ArrayList<Object>();
		params.add(buid);
		return db.finds(sql, params).get(0).get(0);
	}

	public List<BhUser> find(Integer pageNo, Integer pageSize) {
		String sql="select * from(select a.*,rownum rn from (select buid,pwd,bhemail,regDate,status from bhuser order by buid) a where rownum<=?) b where rn>?";
		List<Object> params=new ArrayList<Object>();
		params.add(pageNo*pageSize);
		params.add((pageNo-1)*pageSize);
		return db.find(sql,params,BhUser.class);
	}
	
	public int getTotal() {
		String sql="select count(buid) from bhuser";
		List<List<String>> list=db.finds(sql, null);
		if(list!=null && list.size()>0){
			return Integer.parseInt(list.get(0).get(0));
		}
		return 0;
	}

	public int delsBhUser(String buid) {
		if(buid.indexOf("or")>0){
			return 0;
		}else{
			String sql="delete from bhuser where buid in("+buid+")";
			return db.update(sql, null);
		}
	}
}
